package cn.edu.zut.score.controller.admin;

import cn.edu.zut.score.core.constant.RoleEnum;
import cn.edu.zut.score.core.domain.PageResponse;
import cn.edu.zut.score.core.domain.RestResponse;
import cn.edu.zut.score.core.dto.request.UserPageRequest;
import cn.edu.zut.score.core.dto.response.UserResponse;
import cn.edu.zut.score.core.entity.Info;
import cn.edu.zut.score.core.entity.User;
import cn.edu.zut.score.core.util.MDUtil;
import cn.edu.zut.score.service.IUserService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author zby
 * @date 2021/06/28
 */
@RestController
@RequestMapping("/admin/teacher")
@ApiModel("教师相关ap|i")
@CrossOrigin
public class AdminTeacherController {
    @Autowired
    private IUserService iUserService;

    @ApiOperation("添加教师")
    @PostMapping("")
    public RestResponse<UserResponse> addTeacher(@RequestBody User teacher) {
        teacher.setRole(RoleEnum.teacher.getCode());
        teacher.setPassword(MDUtil.encryption("123456"));
        return iUserService.addTeacher(teacher);
    }

    @ApiOperation("导入教师")
    @PostMapping("/import")
    public RestResponse<Integer> importStudents(MultipartFile file) throws Exception {
        return iUserService.importUsers(file.getInputStream(), RoleEnum.teacher);
    }

    @ApiOperation("根据Id删除教师")
    @DeleteMapping("/{teacherid}")
    public RestResponse<Long> deleteTeacher(@PathVariable("teacherid") Long teacherid) {
        return iUserService.deleteUserById(teacherid);
    }

    @ApiOperation("根据Ids删除教师")
    @DeleteMapping("/list")
    public RestResponse<Integer> deleteTeachers(@RequestBody List<Long> allTeacherIds) {
        return iUserService.deleteUsersById(allTeacherIds);
    }

    @ApiOperation("更新教师信息")
    @PutMapping("")
    public RestResponse<UserResponse> updateTeacher(@RequestBody User teacher) {
        teacher.setRole(RoleEnum.teacher.getCode());
        return iUserService.saveUser(teacher);
    }

    @ApiOperation("重置教师密码")
    @PutMapping("/{teacherid}")
    public RestResponse<UserResponse> resetPassword(@PathVariable("teacherid") Long teacherid) {
        User teacher = new User();
        teacher.setId(teacherid);
        teacher.setRole(RoleEnum.teacher.getCode());
        teacher.setPassword(MDUtil.encryption("123456"));
        return iUserService.saveUser(teacher);
    }

    @ApiOperation("根据Id查询教师")
    @GetMapping("/{teacherid}")
    public RestResponse<UserResponse> queryTeacher(@PathVariable("teacherid") Long teacherid) {
        return iUserService.getUserById(teacherid);
    }

    @ApiOperation("分页查询教师")
    @PostMapping("/list")
    public RestResponse<PageResponse<UserResponse>> queryTeachers(@RequestBody UserPageRequest teacherQuery) {
        teacherQuery.setRole(RoleEnum.teacher.getCode());
        return iUserService.queryUsers(teacherQuery);
    }

    @ApiOperation("查看所有教师")
    @GetMapping("/all")
    public RestResponse<List<Info>> queryAllTeachers() {
        return iUserService.queryAllUsersByRole(RoleEnum.teacher);
    }

}
